package com.snpic.appaw.module.insure.controller.app.cxProvince;

import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.AppCxProvincePageReqVO;
import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.AppCxProvinceRespVO;
import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.AppCxProvinceSaveReqVO;
import jakarta.validation.constraints.NotBlank;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import static com.snpic.appaw.framework.common.pojo.CommonResult.success;

import com.snpic.appaw.framework.excel.core.util.ExcelUtils;

import com.snpic.appaw.framework.apilog.core.annotation.ApiAccessLog;
import static com.snpic.appaw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.cxProvince.CxProvinceDO;
import com.snpic.appaw.module.insure.service.cxProvince.CxProvinceService;

@Tag(name = "C端 - 车险出单配置-市与车险省市映射")
@RestController
@RequestMapping("/insure/cx-province")
@Validated
public class AppCxProvinceController {

    @Resource
    private CxProvinceService cxProvinceService;

    @GetMapping()
    @Operation(summary = "查询投保城市")
    public CommonResult<AppCxProvinceRespVO> getCxProvinceList(@RequestParam @NotBlank(message = "车牌号不能为空") String plateNo) {
        CxProvinceDO cxProvince = cxProvinceService.getCxProvinceByPlateNo(plateNo);
        return success(BeanUtils.toBean(cxProvince, AppCxProvinceRespVO.class));
    }

//    @PostMapping("/create")
//    @Operation(summary = "创建车险出单配置-市与车险省市映射")
    public CommonResult<Long> createCxProvince(@Valid @RequestBody AppCxProvinceSaveReqVO createReqVO) {
        return success(cxProvinceService.createCxProvince(createReqVO));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新车险出单配置-市与车险省市映射")
    public CommonResult<Boolean> updateCxProvince(@Valid @RequestBody AppCxProvinceSaveReqVO updateReqVO) {
        cxProvinceService.updateCxProvince(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除车险出单配置-市与车险省市映射")
//    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCxProvince(@RequestParam("id") Long id) {
        cxProvinceService.deleteCxProvince(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除车险出单配置-市与车险省市映射")
    public CommonResult<Boolean> deleteCxProvinceList(@RequestParam("ids") List<Long> ids) {
        cxProvinceService.deleteCxProvinceListByIds(ids);
        return success(true);
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得车险出单配置-市与车险省市映射")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCxProvinceRespVO> getCxProvince(@RequestParam("id") Long id) {
        CxProvinceDO cxProvince = cxProvinceService.getCxProvince(id);
        return success(BeanUtils.toBean(cxProvince, AppCxProvinceRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得车险出单配置-市与车险省市映射分页")
    public CommonResult<PageResult<AppCxProvinceRespVO>> getCxProvincePage(@Valid AppCxProvincePageReqVO pageReqVO) {
        PageResult<CxProvinceDO> pageResult = cxProvinceService.getCxProvincePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppCxProvinceRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出车险出单配置-市与车险省市映射 Excel")
//    @ApiAccessLog(operateType = EXPORT)
    public void exportCxProvinceExcel(@Valid AppCxProvincePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CxProvinceDO> list = cxProvinceService.getCxProvincePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车险出单配置-市与车险省市映射.xls", "数据", AppCxProvinceRespVO.class,
                        BeanUtils.toBean(list, AppCxProvinceRespVO.class));
    }

}